package com.yanghui.lingyue.functions.ktBase

import com.icedata.sika.bridge.*
import com.icedata.sika.core.CommandProcessor
import com.icedata.sika.core.Sika
import com.yanghui.lingyue.data.Command
import com.yanghui.lingyue.functions.AbstractFunction
import com.yanghui.lingyue.utils.functionUtils.FunctionUtils
import java.util.*

object Probability: AbstractFunction() {
    override val name: String = "Probability"

    override suspend fun parse(event: SikaEvent, sika: Sika): LinkedList<Command> {
        val command = LinkedList<Command>()
        if (event is SikaUserEvent) return command
        event as SikaGroupEvent
        if (!FunctionUtils.atBot(event)) return command
        if (!FunctionUtils.checkValid(event)) return command
        if (event.sendGroup.id !in enableGroup) return command
        val param = FunctionUtils.extractKeyword(event, "概率")
        param?.let {
            command.add(Command(event, name, it))
        }
        return command
    }

    override suspend fun execute(command: Command, sika: Sika): CommandProcessor.Result {
        val result = (0..10000).random().toDouble() / 100.0
        val builder = SikaMessageChainBuilder()
        builder.add(SikaQuoteMessage(command.message))
        builder.add("${command.param} 发生的概率是 ${result}%")
        command.sendGroup?.sendMessage(builder)
        return CommandProcessor.Result.ADOPT
    }
}